package com.glela.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glela.common.constant.BaseConstants;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.UserInfo;
import com.glela.platform.model.request.RequestUserModel;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.model.response.ResponseUser;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;
import com.glela.talent.service.TalentService;

@Controller
@RequestMapping(value = "/talent")
public class TalentController {
	private static Logger logger = LoggerFactory.getLogger(TalentController.class);

	/** 店铺级别角色id集合 */
	private static final int[] storeRoleIds = new int[] { 3, 4, 5, 6, 7, 11 };

	@Autowired
	private TalentService			talentService;
	@Autowired
	private UserService				userService;
	@Autowired
	private BaseRestrictionsService	baseRestrictionsService;

	/**
	 * <dl>
	 * <dt>获取店铺信息</dt>
	 * </dl>
	 * @param request
	 * @return ResponseEntity<?>
	 * @author zls
	 * @date 2017年7月13日 下午9:02:14
	 */
	/*@RequestMapping(value = "/getStoreInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getStoreInfo(@RequestBody RequestUserModel request) {
		try {
			// 获取用户的推荐人信息
			User _talentUser = null;
			// 验证登录用户的ID
			if (ParameterUtils.validParams(request.getUserId())) {
				//				BaseRestrictions xzSisterBaseRestrictions = this.baseRestrictionsService.getBaseRestrictionsForCache(BaseRestrictionsEnum.XZ_SISTER.getText());
				//				if (xzSisterBaseRestrictions == null) {
				//					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
				//				}
				//				_talentUser = userService.getUser(NumberUtils.toLong(xzSisterBaseRestrictions.getExperience() + "", -1l));
				_talentUser = userService.getUser(BaseConstants.XZ_SISTER);
			} else {
				// 获取当前登录的用户信息
				User _loginUser = userService.getUser(request.getUserId());
				if (null == _loginUser) {
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
				}
				// 此用户没有普通推荐人或者普通推荐人不存在，无需展示讲师店铺专区
				if (StringUtils.isEmpty(_loginUser.getRecommendFrom())) {
					//					BaseRestrictions xzSisterBaseRestrictions = this.baseRestrictionsService.getBaseRestrictionsForCache(BaseRestrictionsEnum.XZ_SISTER.getText());
					//					if (xzSisterBaseRestrictions != null) {
					//						UserInfo sisterUserInfo = this.userService.selectUserInfoByUserId(xzSisterBaseRestrictions.getExperience());
					//						if (sisterUserInfo != null) {
					//							_loginUser.setRecommendFrom(sisterUserInfo.getPersionId());
					//						}
					//					}
					UserInfo sisterUserInfo = this.userService.selectUserInfoByUserId(BaseConstants.XZ_SISTER);
					if (sisterUserInfo != null) {
						_loginUser.setRecommendFrom(sisterUserInfo.getPersionId());
					}
					if (StringUtils.isEmpty(_loginUser.getRecommendFrom())) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
					}
				}
				UserInfo userInfo = userService.selectUserInfoByPersionId(_loginUser.getRecommendFrom());
				if (userInfo == null) {
					return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
				}
				if (request.getUserId().longValue() == userInfo.getUserId()) {
					_talentUser = _loginUser;
				} else {
					_talentUser = userService.getUser(userInfo.getUserId());
				}
			}
			if (null == _talentUser) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}

			*//* 如果该用户的推荐人的角色不是34567(学生店主s1 学生店主s2 学生店主s3 学生店主s4 讲师)，就不展示店铺 *//*
			if (!ArrayUtils.contains(storeRoleIds, _talentUser.getRoleId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
			}
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("talentId", _talentUser.getId());// 讲师ID
			dataMap.put("talentHeadPhoto", StringUtils.isEmpty(_talentUser.getPicture()) ? "" : _talentUser.getPicture());// 达人头像
			dataMap.put("talentNickName", StringUtils.isEmpty(_talentUser.getNickName()) ? "" : _talentUser.getNickName());// 达人昵称
			dataMap.put("backgroundImage", _talentUser.getBackgroundImage());// 达人主页背景图
			return new ResponseEntity<>(new ResponseResultModel<>(dataMap), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("APP首页是否展示店铺接口异常：{}", e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}*/
	/**
	 * <dl>
	 * <dt>首页获取店铺信息（新）</dt>
	 * </dl>
	 * @param request
	 * @return ResponseEntity<?>
	 * @author zhangrenjie
	 * @date 2018年1月9日 下午14:02:14
	 */
	@RequestMapping(value = "/getStoreInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> getStoreInfo(@RequestBody RequestUserModel request) {
		try {
			// 获取用户的推荐人信息
			User _talentUser = null;
			if (ParameterUtils.validParams(request.getTalentId())) {//显示当前登录用户绑定的店主店铺
				// 验证登录用户的ID
				if (ParameterUtils.validParams(request.getUserId())) {
//					_talentUser = userService.getUser(BaseConstants.XZ_SISTER);
					_talentUser = userService.getUserById(BaseConstants.XZ_SISTER);
				} else {
					// 获取当前登录的用户信息
//					User _loginUser = userService.getUser(request.getUserId());
					User _loginUser = userService.getUserById(request.getUserId());
					if (null == _loginUser) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
					}
					// 此用户没有普通推荐人或者普通推荐人不存在，无需展示讲师店铺专区
					if (StringUtils.isEmpty(_loginUser.getRecommendFrom())) {
						UserInfo sisterUserInfo = this.userService.selectUserInfoByUserId(BaseConstants.XZ_SISTER);
						if (sisterUserInfo != null) {
							_loginUser.setRecommendFrom(sisterUserInfo.getPersionId());
						}
						if (StringUtils.isEmpty(_loginUser.getRecommendFrom())) {
							return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
						}
					}
					UserInfo userInfo = userService.selectUserInfoByPersionId(_loginUser.getRecommendFrom());
					if (userInfo == null) {
						return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
					}
					if (request.getUserId().longValue() == userInfo.getUserId()) {
						_talentUser = _loginUser;
					} else {
//						_talentUser = userService.getUser(userInfo.getUserId());
						_talentUser = userService.getUserById(userInfo.getUserId());
					}
				}
			}else{//显示分享打开的店主店铺
				_talentUser = userService.getUserById(request.getTalentId());
				/*if (null == _talentUser) {//分享店主的不存在
					User u = userService.getUserById(request.getUserId());
					if (null == u) {//未登录显示校妆姐姐
						_talentUser = userService.getUserById(BaseConstants.XZ_SISTER);
					} else {//已登录显示绑定店主
						if (StringUtils.isBlank(u.getRecommendFrom())) {//未绑定店主
							_talentUser = userService.getUserById(BaseConstants.XZ_SISTER);
						} else {//绑定的店主
							_talentUser = userService.getUserByPersionId(u.getRecommendFrom());
						}
					}
				}*/
			}
			if (null == _talentUser) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}
			/* 如果该用户的推荐人的角色不是34567(学生店主s1 学生店主s2 学生店主s3 学生店主s4 讲师)，就不展示店铺 */
			if (!ArrayUtils.contains(storeRoleIds, _talentUser.getRoleId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29912), HttpStatus.OK);
			}
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("talentId", _talentUser.getId());// 讲师ID
			dataMap.put("talentHeadPhoto", StringUtils.isEmpty(_talentUser.getPicture()) ? "" : _talentUser.getPicture());// 达人头像
			dataMap.put("talentNickName", StringUtils.isEmpty(_talentUser.getNickName()) ? "" : _talentUser.getNickName());// 达人昵称
			dataMap.put("backgroundImage", _talentUser.getBackgroundImage());// 达人主页背景图
			return new ResponseEntity<>(new ResponseResultModel<>(dataMap), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("APP首页是否展示店铺接口异常：{}", e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 推荐用户注册
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/recommendUserRegister", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> recommendUserRegister(@RequestBody RequestUserModel requestUserModel) {
		try {
			if (requestUserModel.getUserName() == null || requestUserModel.getCode() == null
					|| requestUserModel.getRecommendFrom() == null) {
				// 必传参数未传
				return new ResponseEntity<ResponseBaseModel>(new ResponseBaseModel(ErrorEnum.C29901), HttpStatus.OK);
			}
			/*
			 * int i = userService.recommendUserRegister(requestUserModel.getUserName(), requestUserModel.getCode(),
					requestUserModel.getRecommendFrom(), requestUserModel.getNickName(), requestUserModel.getPicture(),
					requestUserModel.getSex());
			*/
			int i = talentService.recommendUserRegister(requestUserModel.getUserName(), requestUserModel.getCode(),
					requestUserModel.getRecommendFrom(), requestUserModel.getNickName(), requestUserModel.getPicture(),
					requestUserModel.getSex());
			// 注册失败
			if (i == 2) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20101), HttpStatus.OK);
			}
			if (i == 3) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20109), HttpStatus.OK);
			}
			//注册后的用户
			ResponseUser user = userService.getUserByName(requestUserModel.getUserName());
			return new ResponseEntity<>(new ResponseResultModel<ResponseUser>(user), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("推荐用户注册异常:", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 自动返还妆币
	 * @param model
	 * @return
	 */
	/*@RequestMapping(value = "/autoBackMoney", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> autoBackMoney(@RequestBody RequestUserModel model) {
		try {
			talentService.autoBackMoney(model.getStartIndex(),model.getPageSize());
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}*/
	
	/**
	 * 数据导入
	 */
	/*@RequestMapping(value = "/autoInsertUser", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> autoInsertUser(@RequestBody RequestUserModel model) {
		try {
			tempDealService.dealDataTorTalentTeacher();
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}*/
	
}
